﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using GongXianDCUI.Models;
using GongXianDCUI.DAL;
using Webdiyer.WebControls.Mvc;

namespace GongXianDCUI.BLL
{
    public class ArticleBLL
    {
        /// <summary>
        /// 根据文章id获取文章实体
        /// </summary>
        /// <param name="articleId"></param>
        /// <returns></returns>
        public Article GetModel(long articleId)
        {
            ArticleDAL dal = new ArticleDAL();
            Article model = dal.GetModel(articleId);
            return model;
        }

        /// <summary>
        /// 查询的是子模块列表
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="categoryId"></param>
        /// <param name="state"></param>
        /// <returns></returns>
        public PagedList<Article> GetArticlePage(int pageIndex, int pageSize, int categoryId, int state)
        {
            ArticleDAL dal = new ArticleDAL();
            decimal total = 0;
            List<Article> list = dal.GetList(categoryId, state, pageIndex, pageSize, out total);
            PagedList<Article> pageList = new PagedList<Article>(list, pageIndex, pageSize, (int)total);
            return pageList;
        }

        public List<Article> GetArticleList(int categoryId)
        {
            return DB.Context.FromSql("select * from  Article where categoryId=" + categoryId + " and    state =1 order by topFlag,hotFlag,pubdate desc").ToList<Article>();
        }

        public List<Article> GetArticleList(int categoryId, bool isPic)
        {
            if (isPic)
            {
                string sql = "select top 5 * from  Article where categoryId=" + categoryId + " and    state =1 and ImageUrl is not null and ImageUrl!='' order by topFlag,hotFlag,pubdate  desc";
                return DB.Context.FromSql(sql).ToList<Article>();
            }
            else
            {
                return null;
            }
        }
        /// <summary>
        /// 分页，查询一个大模块的所有文章 
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="functionId"></param>
        /// <param name="state"></param>
        /// <returns></returns>
        public PagedList<Article> GetArticleModelPage(int pageIndex, int pageSize, int functionId, int state)
        {
            ArticleDAL dal = new ArticleDAL();
            decimal total = 0;
            List<Article> list = dal.GetModelAllArticleList(functionId, state, pageIndex, pageSize, out total);
            PagedList<Article> pageList = new PagedList<Article>(list, pageIndex, pageSize, (int)total);
            return pageList;
        }

    }
}
